package com.nt.train.controller;
import com.nt.train.vo.BasicStudent;
import com.nt.train.vo.ResponseInfo;
import com.nt.train.vo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hlf
 * @version 1.0
 * @date 2022/7/28 8:51
 */
@Slf4j
@RestController
public class HelloController {

    //RequestMapping(value = "/test")
   // @RequestMapping(value = "/test",method = RequestMethod.GET)
   // @GetMapping
    public String sayHello(){
        return "hello spring boot!!" ;
    }

    @RequestMapping("/test2")
    Map<String, Object> getMap() {
        BasicStudent basicStudent = new BasicStudent();
        basicStudent.setId(10);
        basicStudent.setName("222");
        basicStudent.setSex("222");
        basicStudent.setAge(20);

        BasicStudent basicStudent2 = BasicStudent.builder()
                .id(1)
                .name("yorkiiz")
                .sex("male")
                .build();
        System.out.println("basicStudent:");
        System.out.println("basicStudent2:"+basicStudent2);

        Map<String, Object> map = new HashMap<>(3);
        User user = new User(1L, "倪升武", "123456");
        map.put("作者信息", user);
        map.put("basicStudent", basicStudent);
        map.put("basicStudent2", basicStudent2);
        map.put("粉丝数量", 4153);
        return map;
    }

    @RequestMapping("/test3")
    Map<String, Object> getMap3() {
        Map<String, Object> map = new HashMap<>(3);
        User user = new User(1L, "倪升武", "123456");
        map.put("作者信息", user);

        User  user2 = User.builder().id(2L).password("345566").username("李欧").build();
        map.put("user2", user2);

        map.put("粉丝数量", 4153);
        return map;
    }

    @RequestMapping(value = "/second", method = RequestMethod.GET)
    public String second(){
        return "get hello spring boot!!" ;
    }

    @RequestMapping(value = "/third", method = RequestMethod.POST)
    public String third(){
        return " post hello spring boot!!" ;
    }

    @GetMapping("/hello2")
    public String sayHello2(){
        return "hello spring boot!!" ;
    }

    @PostMapping("/hello3")
    public String sayHello3(){
        return "hello spring boot!!" ;
    }


    @RequestMapping("/calc")
    public ResponseInfo<String> calc(Integer id) {
        try {
            // 模拟异常业务代码
            int num = 1 / id;
            log.info("计算结果num={}", num);
            return ResponseInfo.success();
        } catch (Exception e) {
            return ResponseInfo.fail("系统异常，请联系管理员！");
        }
    }

    @RequestMapping("/calc1")
    public ResponseInfo<String> calc1(Integer id) {
        // 模拟异常业务代码
        int num = 1 / id;
        log.info("计算结果num={}", num);
        return ResponseInfo.success();
    }
}
